package com.example.controller.institutioncare.peoplemanagement;

import com.example.bean.institutioncare.peoplemanagement.Peoplemanagement;
import com.example.bean.institutioncare.peoplemanagement.PeoplemanagementDuoTiaoJian;
import com.example.bean.institutioncare.peoplemanagement.RuZhuGuanLiNum;
import com.example.bean.institutioncare.peoplemanagement.ZhuXiao;
import com.example.service.institutioncare.peoplemanagement.PeoplemanagementService;
import com.example.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/ruzhu")
public class PeoplemanagementController {

    @Value("${fileData.staticAccessPath}")
    private String imageSavePath;
    /*** 图片映射路径 */
    @Value("${fileData.uploadRealPath}")
    private String imageMapperPath;

    @Autowired
    PeoplemanagementService peoplemanagementService;

    @PostMapping
    public Result result(@RequestBody PeoplemanagementDuoTiaoJian peoplemanagementDuoTiaoJian){
        Result widpuusad = new Result();
        widpuusad.setData(peoplemanagementService.peoplemanagementPageInfo(peoplemanagementDuoTiaoJian));
        return widpuusad;
    }

    @RequestMapping("/upload")//文件上传
    public String FileUpdate(HttpServletRequest request, MultipartFile file) {
        //配置文件设置想保存的路径
        String str = imageMapperPath;
        //得到上传时的文件名字
        String originalname = file.getOriginalFilename();
        System.out.println("上传时的文件名字:" + originalname);
        String frontNewName = originalname.substring(0, originalname.lastIndexOf("."));
        System.out.println("截取图片名前缀:" + frontNewName);
        String UUIDname = UUID.randomUUID().toString().replaceAll("-","");
        File newFile = new File(str, UUIDname+originalname);

        //获得文件目录，判断是否存在
        if (!newFile.getParentFile().exists()) {
            //如果path路径不存在，创建一个文件夹，存在则使用当前文件夹
            newFile.getParentFile().mkdirs();
        }
        try {
            //保存文件到指定路径之中
            file.transferTo(newFile);
        } catch (IllegalStateException | IOException e) {

            e.printStackTrace();
        }
        System.out.println("上传的文件路径:" + str + UUIDname+originalname);
        return UUIDname+originalname;
    }
    @PostMapping("/zengjia")
    public Result result(@RequestBody RuZhuGuanLiNum ruZhuGuanLiNum){

        Result resulta =  new Result();
        if (peoplemanagementService.haoba(ruZhuGuanLiNum)>0){
            resulta.ok();
        }
        return resulta;
    }

    @GetMapping("/chakan/{noid}")
    public Result resultb(@PathVariable String noid){
        System.err.println("传到后台的noid：  "+noid);
        Result result = new Result();
        result.ok();
        result.setData(peoplemanagementService.ruzhuguanliBean(noid));
        return result;
    }
    @PostMapping("/zhenxiugai")
    public Result xiugai(@RequestBody RuZhuGuanLiNum ruZhuGuanLiNum){
        Result result = new Result();
        if (peoplemanagementService.xiugai(ruZhuGuanLiNum)>0){
            result.ok();
        }else{
            result.setMessage("有误");
        }
        return result;
    }
    @PostMapping("/shanchu")
    public Result shanchu(@RequestBody List<String> list){
        Result result = new Result();
        if (peoplemanagementService.shanchu(list)>0){
            result.ok();
        }else{
            result.setMessage("有误");
        }
        return result;
    }

    @PostMapping("/zhuxiao")
    public Result zhuxiao(@RequestBody ZhuXiao zhuXiao){
        Result result = new Result();
        if (peoplemanagementService.zhuxiao(zhuXiao)>0){
            result.ok();
        }else {
            result.setMessage("有误");
        }
        return result;
    }

    @GetMapping("/xialakuang")
    public Result xialakuang(){
        Result result = new Result();
        result.setData(peoplemanagementService.xialaList());
        return result;
    }

    @GetMapping("/bingzhuangtu")
    public Result bingbing(){
        Result result = new Result();
        result.setData(peoplemanagementService.bingzhuangtu());
        return result;
    }

    @GetMapping("/zhuzhuangtu")
    public Result zhuzhuangtu(){
        Result result = new Result();
        result.setData(peoplemanagementService.zhuzhuangtu());
        return result;
    }
}
